package com.example.springbootredis.controller;


import com.example.eshop.service.CartService;
import com.example.eshop.dto.CartDTO;
import com.example.eshop.common.Result;
import com.example.eshop.vo.CartVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/api/cart")
public class CartController {

    @Autowired
    private CartService cartService;


    @PostMapping("/add")
    public Result addToCart(@Valid @RequestBody CartDTO cartDto) {
        cartService.addToCart(cartDto);
        return Result.success("添加成功");
    }


    @GetMapping("/list")
    public Result getCartList(@RequestParam Long userId) {
        CartVO cartVO = cartService.getCartList(userId);
        return Result.success(cartVO);
    }


    @PostMapping("/updateQuantity")
    public Result updateQuantity(@Valid @RequestBody CartDTO cartDto) {
        cartService.updateQuantity(cartDto);
        return Result.success("更新成功");
    }


    @PostMapping("/delete")
    public Result deleteCartItem(@RequestParam Long cartId) {
        cartService.deleteCartItem(cartId);
        return Result.success("删除成功");
    }


    @PostMapping("/select")
    public Result selectItem(@RequestParam Long cartId, @RequestParam Integer selected) {
        cartService.selectItem(cartId, selected);
        return Result.success("操作成功");
    }


    @PostMapping("/selectAll")
    public Result selectAll(@RequestParam Long userId, @RequestParam Integer selected) {
        cartService.selectAll(userId, selected);
        return Result.success("操作成功");
    }
}